;; init-msys2.el -- 集成MSYS2终端到Emacs

;; 设置MSYS2的路径，根据实际安装位置调整
(defvar msys2-path "D:/msys64")
(defvar msys2-bin (concat msys2-path "/usr/bin"))

;; 设置MSYS2的shell路径
(defvar msys2-shell (concat msys2-bin "/bash.exe"))

;; 创建打开MSYS2终端的函数
(defun run-msys2 ()
  "在Emacs中运行MSYS2终端。"
  (interactive)
  (let ((explicit-shell-file-name msys2-shell)
        (explicit-bash-args '("--login" "-i")))
    (call-interactively 'shell)))

;; 创建打开MSYS2 mintty的函数
(defun run-msys2-mintty ()
  "打开MSYS2的mintty终端。"
  (interactive)
  (start-process "msys2-mintty" nil
                 (concat msys2-path "/mingw64.exe")))

;; 添加MSYS2的bin目录到exec-path
(add-to-list 'exec-path msys2-bin)

;; 设置环境变量
(setenv "PATH" (concat msys2-bin ";" (getenv "PATH")))

;; 为shell-mode设置编码
(add-hook 'shell-mode-hook
          (lambda ()
            (set-buffer-process-coding-system 'utf-8-unix 'utf-8-unix)))

;; 绑定快捷键
(global-set-key (kbd "C-c m") 'run-msys2)
(global-set-key (kbd "C-c M") 'run-msys2-mintty)

(provide 'init-msys2)